import java.util.EnumSet;

public class binarySearch
{
    public static int binarySearch(int[] nums,int target)
    {
        int len = nums.length;
        if(len == 0) return -1;
        if(len == 1) return nums[0]==target ? 0:-1;
        int l = 0;
        int r = len-1;
        if(target < nums[l] || target>nums[r])
        {
            return -1;
        }
        while(l <= r)
        {
            int mid = l+((r-l)>>1);
            if(nums[mid]==target) return mid;
            else if (nums[mid]<target) l=mid+1;
                else                    r=mid-1;
        }
        return -1;
    }

    public static void main(String[] args)
    {
        int[] nums = {1,2,3,4,5,6,7};
        System.out.println(binarySearch(nums,8));
        System.out.println(binarySearch(nums,4));
        System.out.println(binarySearch(nums,0));


    }
}
